package com.alibaba.sample.petstore.dal.dao.cached;

import java.util.List;
import java.util.Map;

import com.alibaba.sample.petstore.dal.dao.CategoryDao;
import com.alibaba.sample.petstore.dal.dataobject.Category;

public class CachedCategoryDao extends AbstractCachedDao<CategoryDao> implements CategoryDao {
    private final Map<String, Category> categoriesById = createCache();
    private List categoryList;

    public CachedCategoryDao(CategoryDao backingDao) {
        super(backingDao);
    }

    public Category getCategoryById(String categoryId) {
        Category cat = categoriesById.get(categoryId);

        if (cat == null) {
            cat = backingDao.getCategoryById(categoryId);
            categoriesById.put(categoryId, cat);
        }

        return cat;
    }

    public List getCategoryList() {
        if (categoryList == null) {
            categoryList = backingDao.getCategoryList();
        }

        return categoryList;
    }
}
